package com.psytech.app.task.web_ui.server;

import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertyPlaceHolderConfigurer extends PropertyPlaceholderConfigurer {

	private static Logger LOG = Logger.getLogger(PropertyPlaceHolderConfigurer.class.getPackage().getName());

	public PropertyPlaceHolderConfigurer(String fileName) {
		if (System.getProperty("taskmanager.web-ui.config.file") != null) {
			fileName = System.getProperty("taskmanager.web-ui.config.file");
		}
		if (!fileName.equals("UNDEFINE")) {
			try {
				LOG.log(Level.FINE, "Config file : " + fileName);
				final PropertiesConfiguration configuration = new PropertiesConfiguration(fileName);
				final Properties properties = new Properties();
				final Iterator keys = configuration.getKeys();
				while (keys.hasNext()) {
					final String key = (String) keys.next();
					LOG.log(Level.FINE, key + " : " + (String) configuration.getProperty(key));
					properties.setProperty(key, (String) configuration.getProperty(key));
				}
				this.setProperties(properties);

			} catch (final ConfigurationException e) {
				throw new RuntimeException(e);
			}
		} else
			throw new RuntimeException(
					"please set the system properties or the jndi entry taskmanager.web-ui.config.file");

	}
}
